<ul>
<?php

define('MAX_UPLOAD_SIZE', 64000); // maximum allowed office doc for upload
$target_path = "_uploads/";
$work_path = "_workdir/";
$form_field_name = 'upload_file';

if (!is_writable($target_path)) {
  echo "<li>Upload directory is not writable!";
  exit;
 }

if (!is_writable($work_path)) {
  echo "<li>Work dir is not writable!";
  exit;
 }

// check if uploaded
// a. take the uploaded file


if (!array_key_exists($form_field_name, $_FILES)) {
  echo "Upload failed, got no file.<p>Please <a href=\"upload.html\">try again</a>!";
  exit;
}


$target_path = $target_path . basename( $_FILES[$form_field_name]['name']); 

if (filesize($_FILES[$form_field_name]['tmp_name']) > MAX_UPLOAD_SIZE) {
  echo "The file '" . basename( $_FILES[$form_field_name]['name']). "' exceeds the maximum size of " . MAX_UPLOAD_SIZE . " bytes. Please <a href=\"upload.html\">try again</a>!";
  exit;
}

if(move_uploaded_file($_FILES[$form_field_name]['tmp_name'], $target_path)) {
  echo "<li> The file ".  basename( $_FILES[$form_field_name]['name']). 
    " has been uploaded"; 
  //rename($target_path, $work_path . basename($_FILES[$form_field_name]['name']));
  //$target_path = $work_path . basename($_FILES[$form_field_name]['name']);
} else{
  echo "There was an error uploading the file, please <a href=\"upload.html\">try again</a>!";
}

print "<li> File size " . filesize($target_path);

// ---------------------------------------------------------------------
// b. extract, since it is a zip -file

unlink($work_path . "*");
$zip = new ZipArchive;
if ($zip->open($target_path) === TRUE) {
  $zip->extractTo($work_path);
  $zip->close();
  echo "<li>unzipped the file.";
 }

// c. extract images, according to XX xml
// d. extract styles
// e. extract textual content and enumerate it according to the structure


// 1. create a new revision branch, if it is a new doc
// 2. run a diff and simplify, ie. remove redunduncies
?>

</ul>


